home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 10 / AACD 10.iso / AACD / Games / MAME / src / sndhrdw / cclimber.c < prev    next >
C/C++ Source or Header  |  2000-04-04  |  2KB  |  92 lines

  1. #include "driver.h"
  2.  
  3.  
  4. /* macro to convert 4-bit unsigned samples to 8-bit signed samples */
  5. #define SAMPLE_CONV4(a) (0x11*((a&0x0f))-0x80)
  6.  
  7. #define SND_CLOCK 3072000    /* 3.072 Mhz */
  8.  
  9.  
  10. static signed char *samplebuf;    /* buffer to decode samples at run time */
  11. static int channel;
  12.  
  13.  
  14.  
  15. int cclimber_sh_start(const struct MachineSound *msound)
  16. {
  17.     channel = mixer_allocate_channel(50);
  18.     mixer_set_name(channel,"Samples");
  19.  
  20.     samplebuf = 0;
  21.     if (memory_region(REGION_SOUND1))
  22.     {
  23.         samplebuf = malloc(2*memory_region_length(REGION_SOUND1));
  24.         if (!samplebuf)
  25.             return 1;
  26.     }
  27.  
  28.     return 0;
  29. }
  30.  
  31. void cclimber_sh_stop(void)
  32. {
  33.     if (samplebuf)
  34.         free(samplebuf);
  35.     samplebuf = NULL;
  36. }
  37.  
  38.  
  39.  
  40. static void cclimber_play_sample(int start,int freq,int volume)
  41. {
  42.     int len;
  43.     const UINT8 *rom = memory_region(REGION_SOUND1);
  44.  
  45.  
  46.     if (!rom) return;
  47.  
  48.     /* decode the rom samples */
  49.     len = 0;
  50.     while (start + len < memory_region_length(REGION_SOUND1) && rom[start+len] != 0x70)
  51.     {
  52.         int sample;
  53.  
  54.         sample = (rom[start + len] & 0xf0) >> 4;
  55.         samplebuf[2*len] = SAMPLE_CONV4(sample) * volume / 31;
  56.  
  57.         sample = rom[start + len] & 0x0f;
  58.         samplebuf[2*len + 1] = SAMPLE_CONV4(sample) * volume / 31;
  59.  
  60.         len++;
  61.     }
  62.  
  63.     mixer_play_sample(channel,samplebuf,2 * len,freq,0);
  64. }
  65.  
  66.  
  67. static int sample_num,sample_freq,sample_volume;
  68.  
  69. WRITE_HANDLER( cclimber_sample_select_w )
  70. {
  71.     sample_num = data;
  72. }
  73.  
  74. WRITE_HANDLER( cclimber_sample_rate_w )
  75. {
  76.     /* calculate the sampling frequency */
  77.     sample_freq = SND_CLOCK / 4 / (256 - data);
  78. }
  79.  
  80. WRITE_HANDLER( cclimber_sample_volume_w )
  81. {
  82.     sample_volume = data & 0x1f;    /* range 0-31 */
  83. }
  84.  
  85. WRITE_HANDLER( cclimber_sample_trigger_w )
  86. {
  87.     if (data == 0 || Machine->sample_rate == 0)
  88.         return;
  89.  
  90.     cclimber_play_sample(32 * sample_num,sample_freq,sample_volume);
  91. }
  92.